package com.woniuxy.sale.receiver;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;


import com.woniuxy.commons.entity.sale.ChooseProduceMsg;
import com.woniuxy.commons.rabbit.GoldService;
import com.woniuxy.commons.rabbit.RabbitMqConst;
import com.woniuxy.sale.service.PackingMsgService;
import com.woniuxy.sale.service.ProductService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO: mq监听器 监听修改商品信息库存
 * <P> Date: 2022/4/21:10:40
 * <P> Description:
 */
@Component
public class SaleReceiver {


    @Resource
    private GoldService goldService;

    @Resource
    private ProductService productService;

    @Resource
    private PackingMsgService packingMsgService;

    /**
     *
     * TODO 绑定对应的队列 交换机 路由名 短信对应
     *      durable = "true" 是否持久化
     *      这里：调用方法修改库存
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitMqConst.QUEUE_PRODUCT, durable = "true"),
            exchange = @Exchange(value = RabbitMqConst.EXCHANGE_DIRECT_PRODUCT),
            key = {RabbitMqConst.ROUTING_PRODUCT}
    ))
    public void receiver(List<ChooseProduceMsg> chooseProduceMsgs,
                         Message message, Channel channel)  {

        productService.updateAmountById(chooseProduceMsgs);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitMqConst.QUEUE_CHOOSE_PRODUCE_MSG, durable = "true"),
            exchange = @Exchange(value = RabbitMqConst.EXCHANGE_DIRECT_PRODUCT_UPDATE),
            key = {RabbitMqConst.ROUTING_CHOOSE_PRODUCE_MSG}
    ))
        public void updatePackingStatus(Integer psId,
                                    Message message, Channel channel) throws IOException {

        packingMsgService.updatePackingStatus(psId);
    }


}
